<template>
  <div>
    <dynamic-crud title="现金事务" :data="list" :option="option"
      :tableLoading="tableLoading" :disabled="isView" :rowAdd="rowadd">
    </dynamic-crud>
    <div class="bottom-menu" v-if="!isView">
<!--      <el-button type="primary" size="small" :disabled="btn" @click="handelSubmit" :loading="isSubmit">保 存</el-button>-->
<!--      <el-button size="small" @click="handleCancle" :loading="isSubmit">取 消</el-button>-->
    </div>
  </div>
</template>

<script>
  import {fetchList, addObj, putObj, getTicketTypes, getTicketSubTypeList} from '@/api/subwaystm/swstinputpassengerwork/cash-work';
  import { dateFormat } from '@/util/date';
  import {getPosTypes, getPosCardTypes} from "@/api/subwayRat/commonCheck";
  export default {
    name: 'preview-cash-work',
    inject: ['passengerWork'],
    data() {
      return {
        // posType: '1',
        list: [],
        isSubmit: false,
        tableLoading: false,
        relationId: null,
        btn: false,
        posTypes:[],
        ticketTypes:[],
        ticketSubTypes:[],
        posCardTypes:[],
        disabledValue: false,
        option: {
          column: [{
            label: '事件详情',
            prop: 'eventDetail',
            type: 'select',
            dicUrl: '/admin/dict/type/cash_work_event_detail',
            cell: true,
            filterable: true,
            allowCreate: false,
            width: 150,
            // change: this.handleEventDetailChange
          }, {
            label: '处理结果',
            prop: 'handleResult',
            cell: true,
            width: 330
          },{
               label: '金额',
               prop: 'amount',
               type: 'number',
               precision: 2,
               cell: true,
               step:0,
               width: 110
            },{
               label: '备注',
		prop: 'remark',
		type: 'input',
		cell: true,
		width: 140
            }, {
               label: '上传截图',
		prop: 'filePath',
		type: 'upload',
		component: 'zd-upload',
		cell: true,
		hide: true
             }, {
                label: '事务发生时间',
		prop: 'workTime',
		type: 'datetime',
		format: 'yyyy-MM-dd HH:mm:ss',
		valueFormat: 'yyyy-MM-dd HH:mm:ss',
		cell: true,
		width: 210
             },{
            label: 'POS交易类型',
            prop: 'posType',
            type: 'select',
            dicUrl: '/admin/dict/type/cash_work_pos_type',
            // cell: this.isHandleResultEditable,
            cell: true,
            // disabled: true,
            filterable: true,
            allowCreate: false,
            width: 110
          } ,{
            label: '票种',
            prop: 'ticketType',
            type: 'select',
            cell: true,
            cascaderItem: ["ticketSubType"],
            dicUrl: '/support/swpmticketcardtype/dict/ticketPosCash',
            props: {
              label: 'mainName',
              value: 'mainEnName'
            },
              width: 120
	   },{
	      label: '车票名称',
	      prop: 'ticketSubType',
	      type: 'select',
	      dicUrl: '/support/swpmticketcardtypeparam/dictAll/{{key}}',
	      props: {
              label: 'subName',
              value: 'subEnName'
	      },
              cell:true,
	      // component: 'ticket-name-select',
	      width: 130
	    },{
	      label: '数量',
	      prop: 'posSum',
	      type: 'number',
              cell:true,
	      step:0,
	      width: 110
            },{
          "type": "select",
          "label": "POS卡类型",
          "prop": "posCardType",
           dicUrl:'/admin/dict/type/pos_card_type' ,
           width: 180,
           cell:true
	    }]
         }
      }
    },
    computed: {
      isView(){
        return this.boxType == 'view'
      }
    },
    props: {
      workInfo: Object,
      boxType: String
    },
    mounted() {
      getPosTypes('cash_work_pos_type').then(res=>{
        this.posTypes=res;
      });
      getTicketTypes().then(res=>{
        this.ticketTypes=res.data.data;
      });
      getPosCardTypes('pos_card_type').then(res=>{
        this.posCardTypes=res;
      });
    },
    methods: {
      getTicketSubTypes(ticketType){
        getTicketSubTypeList(ticketType).then(res=>{
          this.ticketSubTypes = res.data.data;
        });
      },
      // getTicketTypeDisabled(posType){
      //   console.log(posType)
      //   this.posType = posType;
      // },
      rowadd: function(callback){
        let now = new Date()
        if(now.getHours() < 6){
          now.setDate(now.getDate() - 1)
        }
        callback({
          "workTime":dateFormat(now)
        })
      },
      handleEventDetailChange({ value }) {
        console.log("值改变", value);
        this.disabledValue = (value === '9');
        console.log("disabledValue返回", !this.disabledValue)
      },
      isHandleResultEditable() {
        // 根据 disabledValue 返回 true 或 false
        return !this.disabledValue;
      },
      getList(){
        if(this.relationId != this.workInfo.id){
          this.list = []
          this.tableLoading = true
          fetchList(this.workInfo.id).then(response => {
            this.relationId = this.workInfo.id
            this.list = response.data.data
            this.list.forEach(l => l.$cellEdit = true)
            this.tableLoading = false
          })
        }
      },
      clear(){
        this.list = []
      },
      handelSubmit(){
        var params = {
          ...this.workInfo,
          workList: this.list
        }
        // if (this.eventDetail != 9 && (this.posType != '' || this.ticketType != '' || this.ticketSubType != '' || this.posSum != '' || this.posCardType != '')) {
        //   this.$message.info('非POS机事务，不能选择POS机相关信息！')
        //   return
        // }
        if(this.boxType == 'add'){
          this.passengerWork.validateForm((valid)=>{
            if(valid){
              if(this.list.length == 0){
                this.$message.info('请添加现金事务')
                return
              }else{
                for(const item of this.list){
                  if(item.eventDetail == null || item.handleResult ==null || item.amount ==null || item.workTime == null){
                    this.$message.info("事件详情、处理结果、金额、事务发生时间为必填项，不能为空！")
                    return
                  }
                  if (
                    item.eventDetail == 2 ||
                    item.eventDetail == 3
                  ) {
                    if (item.amount > 0) {
                      this.$message.info("请检查当前事件的金额应该为负数！");
                      return;
                    }
                  }
                }
              }
              this.isSubmit = true
              addObj(params).then(response => {
                this.$message.success('添加成功')
                this.btn = true
                this.isSubmit = false
		this.passengerWork.getList(this.passengerWork.page)//20240827 测试  点击保存成功后刷新主页面
              })
            }
          })
        }else{
          if(this.list.length > 0){
            for(const item of this.list){
              if(item.eventDetail == null || item.handleResult ==null || item.amount ==null || item.workTime == null){
                this.$message.info("事件详情、处理结果、金额、事务发生时间为必填项，不能为空！")
                return
              }
              if (
                item.eventDetail == 2 ||
                item.eventDetail == 3
              ) {
                if (item.amount > 0) {
                  this.$message.info("请检查当前事件的金额应该为负数！");
                  return;
                }
              }
            }
          }
          this.isSubmit = true
          putObj(params).then(response => {
            this.$message.success('修改成功')
            this.btn = true
            this.isSubmit = false
          })
        }
      },
      handleCancle(){
        if(this.boxType == 'mergeEdit'){
          this.passengerWork.closeDialogMerge()
        }else{
          this.passengerWork.closeDialog()
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  .bottom-menu{
    margin-top: 30px;
    display: flex;
    justify-content: center
  }
</style>
